/**
 * Created by li on 16-2-17.
 */

var mongo = require('mongodb'),
    Server = mongo.Server,
    Db = mongo.Db;
var init={
    init:function(host,port,db){
        link = new Db(db,new Server(host,port,{auto_reconnect:true}));
        return link;
    }
}
var mongo={
    insert:function(mongodb,collection,obj){
        mongodb.open(function(err, db) {
            if(!err) {
                db.collection(collection, function(err, collection) {
                   // console.log(obj);
                    collection.insert(obj);
                });
            }
        });
    },
    search:function(mongodb,collection,condition){

        mongodb.open(function(err, db) {
            if(!err) {
                db.collection(collection, function(err, collection){
                    collection.find(condition).toArray(function(error, bars){
                        if(error){
                            console.log(error)
                        }
                        console.log(bars);
                    });
                });
            }
        });
    }
};

exports.mongo=mongo;
exports.mongodb=init;